package com.warren.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @version V1.0
 * @Description: 发送邮件
 * @author: warren
 * @date: 2022/4/1$ 15:26$
 */
@Service
@Slf4j
public class MailService {
    @Autowired
    JavaMailSenderImpl mailSender;

    public void sendSimple() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("hello");
        simpleMailMessage.setText("xixi ahha");
        simpleMailMessage.setTo("misswo@163.com");
        simpleMailMessage.setFrom("misswo@163.com");
        mailSender.send(simpleMailMessage);
    }

    public void sendMime() {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("hello");
            helper.setText("<p style='color: red'>hello</p>", true);


            //附件
            helper.addAttachment("1.jpg", new File("D:\\nike\\hello-world\\abc.txt"));
            helper.setTo("misswo@163.com");
            helper.setFrom("misswo@163.com");

            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送失败", e.getMessage());
        }
    }
}
